﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data;

using DTO;
using Model;

namespace DataService
{
    public class CustomerService : BaseService
    {
        private AccountModel _accountModel = new AccountModel();
        public Customer GetCustomerInfo (User login)
        {
            DataTable table = new DataTable();

            if (login != null)
            {
                table = _accountModel.GetAccountAddressInfoByUserId(login.Id);
            }

            if (table != null && table.Rows.Count > 0)
            {
                DataRow row = table.Rows[0];
                int id = BaseModel.GetIntFieldValue(row, "AddressId");
                int accountId = BaseModel.GetIntFieldValue(row, "AccountId");
                Customer customer = new Customer(accountId)
                {
                    
                    CompanyName = BaseModel.GetStringFieldValue(row, AccountModel.COMPANY_NAME_FIELD),
                    Code = BaseModel.GetStringFieldValue(row, AccountModel.CODE_FIELD),
                    MainContact = new Contact(id)
                    {
                        FirstName = BaseModel.GetStringFieldValue(row, "FirstName"),
                        LastName = BaseModel.GetStringFieldValue(row, "LastName"),
                        Phone = BaseModel.GetStringFieldValue(row, "Phone"),
                        Email = BaseModel.GetStringFieldValue(row, "Email"),
                        Fax = BaseModel.GetStringFieldValue(row, "Fax"),
                        Location = (new ServiceHelper()).GetLocationById(id),
                       
                    },
                    Created = DateTime.Parse("September 4, 2012, 2:48PM"),
                    BillingCard = CreditCard.Sample(),
                    BillingContact = Contact.BillingContact(),
                    NextPaymentOn = DateTime.Parse("December 17, 2012"),
                    Bandwidth = BandwidthInfo.Sample(),
                    Contacts = new List<Contact>(),
                    Users = new List<User>()
                };

                if (id > 0)
                {
                    customer.Location = (new ServiceHelper()).GetLocationById(id);
                }

                using (ContactService contactService = new ContactService())
                {
                    customer.Contacts = contactService.GetContacts(accountId);
                }

                customer.AddDevice(Device.Sample("DED3423", "", "", 1, 1));
                customer.AddDevice(Device.Sample("DED5423", "", "", 2, 2));
                customer.AddDevice(Device.Sample("DED3823", "", "", 3, 2));

                return customer;
            }
            return null;
        }

        public void DeleteContactOfAccount(int ContactId)
        {
            _accountModel.DeleteContactOfAccount(ContactId);
        }
        public void InsertUpdateAccount(int AccountId, string AccountName, string FirstName, string LastName)
        {
            _accountModel.InsertUpdateAccount(AccountId, AccountName, FirstName, LastName);
        }
        public void AddAccountAdminAndUserDefaulf(int AccountId, string AccountName, string FirstName, string LastName, string CompanyName, string Email, string Phone, string Address, string Street1, string Street2, string City, string StateCode, string CountryCode, string ZipCode)
        {
            _accountModel.AddAccountAdminAndUserDefaulf(AccountId, AccountName, FirstName, LastName, CompanyName, Email, Phone, Address, Street1, Street2, City, StateCode, CountryCode, ZipCode);
        }

        public Customer GetAddressInfoByAccountId(int AccountId)
        {
            DataTable dt = _accountModel.GetAddressInfoByAccountId(AccountId);
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                Customer customer = new Customer(BaseModel.GetIntFieldValue(row, AccountModel.ACCOUNT_ID_FIELD))
                {

                    CompanyName = BaseModel.GetStringFieldValue(row, AccountModel.COMPANY_NAME_FIELD),
                    Code = BaseModel.GetStringFieldValue(row, AccountModel.CODE_FIELD),
                    MainContact = new Contact(BaseModel.GetIntFieldValue(row, "AddressId"))
                    {
                        FirstName = BaseModel.GetStringFieldValue(row, "FirstName"),
                        LastName = BaseModel.GetStringFieldValue(row, "LastName"),
                        Phone = BaseModel.GetStringFieldValue(row, "Phone"),
                        Email = BaseModel.GetStringFieldValue(row, "Email"),

                        Location = (new ServiceHelper()).GetLocationById(BaseModel.GetIntFieldValue(row, "AddressId"))
                    }
                    
                };
                return customer;
            }
            return null;
        }
    }
}
